home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Lose Your Marbles! 1.0 / source / Shell ƒ / buttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  5.7 KB  |  200 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        buttons.c
  4.  
  5. Purpose:    This module handles 3D buttons: drawing, clicking, tracking.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "buttons.h"
  25.  
  26. static    RGBColor        gBlack4Bit={32768, 32768, 32768};
  27. static    RGBColor        gWhite4Bit={60948, 60948, 60948};
  28. static    RGBColor        gBackground4Bit={49152,49152,49152};
  29. static    RGBColor        gBlack8Bit={26214, 26214, 39322};
  30. static    RGBColor        gWhite8Bit={60948, 60948, 60948};
  31. static    RGBColor        gBackground8Bit={52429, 52429, 52429};
  32.  
  33. void Draw3DButton(Rect *buttonRect, unsigned char *theTitle,
  34.     Handle iconHandle, short buttonDepth, Boolean isDown)
  35. {
  36.     FontInfo        theFontInfo;
  37.     Rect            tempRect;
  38.     RGBColor        oldForeColor, oldBackColor;
  39.     PixPatHandle    backgroundppat;
  40.     short            theLineHeight;
  41.     Rect            iconRect;
  42.     RGBColor        myWhiteColor={65535, 65535, 65535};
  43.     
  44.     TextFont(geneva);
  45.     TextSize(9);
  46.     TextFace(0);
  47.     GetFontInfo(&theFontInfo);
  48.     theLineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  49.     if (iconHandle!=0L)
  50.     {
  51.         iconRect.left=(buttonRect->right-buttonRect->left)/2+buttonRect->left-16+
  52.             (isDown ? 1 : 0);
  53.         iconRect.right=iconRect.left+32;
  54.         iconRect.top=(buttonRect->bottom-buttonRect->top)/2+buttonRect->top-16+
  55.             (isDown ? 1 : 0)-((theTitle!=0L) ? 5 : 0);
  56.         iconRect.bottom=iconRect.top+32;
  57.     }
  58.     
  59.     if (buttonDepth>2)
  60.     {
  61.         GetForeColor(&oldForeColor);
  62.         GetBackColor(&oldBackColor);
  63.         
  64.         backgroundppat=NewPixPat();
  65.         MakeRGBPat(backgroundppat, (buttonDepth==4) ? &gBackground4Bit :
  66.             &gBackground8Bit);
  67.         SetRect(&tempRect, buttonRect->left+2, buttonRect->top+2,
  68.             buttonRect->right-2, buttonRect->bottom-2);
  69.         
  70.         FillCRect(&tempRect, backgroundppat);
  71.  
  72.         RGBBackColor(&myWhiteColor);
  73.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit) :
  74.             ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit));
  75.         MoveTo(buttonRect->left, buttonRect->top);
  76.         LineTo(buttonRect->right-2, buttonRect->top);
  77.         MoveTo(buttonRect->left, buttonRect->top+1);
  78.         LineTo(buttonRect->right-3, buttonRect->top+1);
  79.         MoveTo(buttonRect->left, buttonRect->top+2);
  80.         LineTo(buttonRect->left, buttonRect->bottom-2);
  81.         MoveTo(buttonRect->left+1, buttonRect->top+2);
  82.         LineTo(buttonRect->left+1, buttonRect->bottom-3);
  83.         
  84.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit) :
  85.             ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit));
  86.         MoveTo(buttonRect->right-1, buttonRect->top);
  87.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  88.         MoveTo(buttonRect->right-2, buttonRect->top+1);
  89.         LineTo(buttonRect->right-2, buttonRect->bottom-1);
  90.         MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  91.         LineTo(buttonRect->right-1, buttonRect->bottom-2);
  92.         MoveTo(buttonRect->left+1, buttonRect->bottom-1);
  93.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  94.     }
  95.     else
  96.     {
  97.         EraseRect(buttonRect);
  98.         FrameRect(buttonRect);
  99.         if (!isDown)
  100.         {
  101.             MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  102.             LineTo(buttonRect->right-2, buttonRect->bottom-2);
  103.             LineTo(buttonRect->right-2, buttonRect->top+2);
  104.         }
  105.         else
  106.         {
  107.             MoveTo(buttonRect->left+1, buttonRect->bottom-3);
  108.             LineTo(buttonRect->left+1, buttonRect->top+1);
  109.             LineTo(buttonRect->right-3, buttonRect->top+1);
  110.         }
  111.     }
  112.     
  113.     if (theTitle!=0L)
  114.     {
  115.         ForeColor(blackColor);
  116.         MoveTo(buttonRect->left+(buttonRect->right-buttonRect->left)/2-
  117.             (StringWidth(theTitle)/2)+(isDown ? 1 : 0), ((iconHandle!=0L) ?
  118.             buttonRect->bottom-6 : buttonRect->top+
  119.             (buttonRect->bottom-buttonRect->top)/2+theLineHeight/2-2)+
  120.             (isDown ? 1 : 0));
  121.         DrawString(theTitle);
  122.     }
  123.     
  124.     if (iconHandle!=0L)
  125.     {
  126.         if (buttonDepth>2)
  127.             PlotCIcon(&iconRect, (CIconHandle)iconHandle);
  128.         else
  129.         {
  130.             BitMap    iconMap;
  131.             GrafPtr curPort;
  132.             
  133.             GetPort(&curPort);
  134.             HLock( iconHandle);    /* lock data in place */
  135.             
  136.             iconMap.baseAddr = *iconHandle;    /* dereference the handle */
  137.             iconMap.rowBytes = 4;            /* setup other fields */
  138.             SetRect( &iconMap.bounds, 0,0,32,32);
  139.             
  140.             CopyBits( &iconMap, &(curPort->portBits),
  141.                          &iconMap.bounds, &iconRect, srcOr, 0L );
  142.             
  143.             HUnlock(iconHandle);                /* all done; let it float */
  144.         }
  145.     }
  146.     
  147.     if (buttonDepth>2)
  148.     {
  149.         DisposPixPat(backgroundppat);
  150.         RGBForeColor(&oldForeColor);
  151.         RGBBackColor(&oldBackColor);    
  152.     }
  153. }
  154.  
  155. Boolean Track3DButton(Rect *buttonRect, unsigned char *theTitle,
  156.     Handle iconHandle, short buttonDepth)
  157. {
  158.     Point            mouseLoc;
  159.     Boolean            buttonIsDown;
  160.     
  161.     buttonIsDown=FALSE;
  162.     while (StillDown())
  163.     {
  164.         GetMouse(&mouseLoc);
  165.         if (PtInRect(mouseLoc, buttonRect))
  166.         {
  167.             if (!buttonIsDown)
  168.             {
  169.                 buttonIsDown=TRUE;
  170.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  171.                     TRUE);
  172.             }
  173.         }
  174.         else
  175.         {
  176.             if (buttonIsDown)
  177.             {
  178.                 buttonIsDown=FALSE;
  179.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  180.                     FALSE);
  181.             }
  182.         }
  183.     }
  184.     
  185.     if (buttonIsDown)
  186.         Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE);
  187.     
  188.     return buttonIsDown;
  189. }
  190.  
  191. void Hit3DButton(Rect *buttonRect, unsigned char *theTitle,
  192.     Handle iconHandle, short buttonDepth)
  193. {
  194.     long            dummy;
  195.     
  196.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, TRUE);
  197.     Delay(8, &dummy);
  198.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE);
  199. }
  200.